using GPC_Overlay;
using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace GISWebRole
{
    public class WebRole : RoleEntryPoint 
    {
        public override bool OnStart()
        {
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
            {
                // Provide the configSetter with the initial value
                configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));

                RoleEnvironment.Changed += (sender, arg) =>
                {
                    foreach (var change in arg.Changes)
                    {
                        var configChange = change as RoleEnvironmentConfigurationSettingChange;
                        if (configChange != null)
                        {
                            RoleEnvironment.GetConfigurationSettingValue(
                                configChange.ConfigurationSettingName);
                        }
                    }
                };
            });
            
            var diag = DiagnosticMonitor.GetDefaultInitialConfiguration();

            diag.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
            diag.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            diag.WindowsEventLog.DataSources.Add("Application!*");
            diag.WindowsEventLog.DataSources.Add("System!*");
            diag.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            diag.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", diag);
            
            string configurationstring = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString");
            QueueHelper queueObject = new QueueHelper(configurationstring);
            queueObject.CreateQueue(DataConstants.RESULT_QUEUE);
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE);
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE+"1a");
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE+"2a");
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE+"3a");
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE+"4a");
            queueObject.CreateQueue(DataConstants.WORKER_QUEUE+"5a");
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE);
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE + "1a");
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE + "2a");
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE + "3a");
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE + "4a");
            queueObject.ClearMessages(DataConstants.WORKER_QUEUE + "5a");
            queueObject.ClearMessages(DataConstants.RESULT_QUEUE);
            BlobHelper blobObject = new BlobHelper(configurationstring);
            //blobObject.CreateContainer(DataConstants.MAP_CONTAINER_BLOB);

            return base.OnStart();
        }
    }
}
